import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

const login = () => import('../views/login/login.vue')
const Reg = () => import('@/views/Reg/Reg.vue')
const Main = () => import('@/views/Main/Main.vue')
const Home = () => import('@/views/menus/Home/Home.vue')
const userinfo = () => import('@/views/menus/User/userinfo.vue')
const userAvatar = () => import('@/views/menus/User/userAvatar.vue')
const userpwd = () => import('@/views/menus/User/userpwd.vue')
const ArtCate = () => import('@/views/menus/article/ArtCate.vue')
const ArtList = () => import('@/views/menus/article/ArtList.vue')
Vue.use(VueRouter)

const routes = [
  { path: '/reg', component: Reg },
  { path: '/login', component: login },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-info', component: userinfo },
      { path: 'user-avatar', component: userAvatar },
      { path: 'user-pwd', component: userpwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  const whiteList = ['/login', '/reg']
  if (!store.state.user.token && !whiteList.includes(to.path)) {
    Vue.prototype.$message.error('您没登录啊')
    return next('/login')
  }
  next()
})

export default router
